package com.gs.disk.controller.common;

import com.gs.entity.R;
import com.gs.common.util.SecurityUtils;
import com.gs.entity.MenuEntity;
import com.gs.disk.service.common.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("disk/common/menu")
@Api(tags = "系统菜单")
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 根据用户信息获取菜单
     * @return
     */
    @GetMapping("userMenu")
    @ApiOperation("根据用户信息获取菜单")
    public R<List<MenuEntity>> userMenu() {
        Long userId = SecurityUtils.getUserId();
        List<MenuEntity> menuEntities = menuService.userMenu(userId);
        return R.ok(menuEntities);
    }

    /**
     * 获取菜单树形结构
     * 显示所有的菜单, 不管是否启用, 是否隐藏
     * @return
     */
    @GetMapping("listTree")
    @ApiOperation("获取菜单树形结构")
    public R<List<MenuEntity>> listTree() {
        List<MenuEntity> menuEntities = menuService.listTree();
        return R.ok(menuEntities);
    }

    @PutMapping("saveMenu")
    @ApiOperation("保存新的菜单")
    @RequiresPermissions({"sys:menu:update", "sys:menu:add"})
    public R saveMenu(@RequestBody MenuEntity menuEntity) {
        menuService.saveMenu(menuEntity);
        return R.ok();
    }



    @DeleteMapping("deleteMenu/{id}")
    @ApiOperation("删除菜单")
    @RequiresPermissions({"sys:menu:delete"})
    public R deleteMenu(@PathVariable Long id) {
        menuService.deleteMenu(id);
        return R.ok();
    }
}
